import 'package:flutter/material.dart';

class HomeView extends StatefulWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> with SingleTickerProviderStateMixin {
  final tabs = [Tab(text: '热门'), Tab(text: '新闻'), Tab(text: '娱乐'), Tab(text: '明星')];

  final tabBarViews = [
    Center(child: Text('热门页面')),
    Center(child: Text('新闻页面')),
    Center(child: Text('娱乐页面')),
    Center(child: Text('明星页面'))
  ];

  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
    _tabController.addListener(() {
      print("当前点击的是第${_tabController.index}个tab");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: TabBar(
          controller: _tabController,
          tabs: tabs,
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: tabBarViews,
      ),
    );
  }
}
